<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class ActivityController extends Controller{
    
    
   public function actionIndex()
    {
        $new =time();
            $sqlActivity = "SELECT a.id,a.title,als.content,a.start_time,a.type_id,als.address,a.min_banner_url,a.fav_count,a.signup_count,t.name FROM xyh_activity as a"
                         . " LEFT JOIN xyh_activity_link_signup as als on a.id = als.activity_id"
                         . " LEFT JOIN xyh_type as t on a.cat_id = t.id"
                         . " WHERE a.status=1 and a.is_del=2 and a.end_time >{$new}  and  a.is_hot=2 ORDER BY a.id DESC  ";
   
            $dataAc = Yii::app()->db->createCommand($sqlActivity)->queryAll();
            
            //p_e($dataAc);
            
            $criteria=new CDbCriteria();
            $result = Yii::app()->db->createCommand($sqlActivity)->query();
            $pages=new CPagination($result->rowCount);
            $pages->pageSize=5; 
            $pages->applyLimit($criteria); 
            $result=Yii::app()->db->createCommand($sqlActivity." LIMIT :offset,:limit"); 
            $result->bindValue(':offset', $pages->currentPage*$pages->pageSize); 
            $result->bindValue(':limit', $pages->pageSize); 
            $dataAc=$result->queryAll();
                    
            // p_e($dataAc);
            
            foreach ($dataAc as $k=>$v){
                
                 $dataAc[$k]['content']  =mb_substr(strip_tags($this->trimall($dataAc[$k]['content'])),0,150,'utf-8'); 
                 $dataAc[$k]['start_day'] = $this->TimeDifference(time(),$v['start_time']);//计算几天开始 空为已开始
                 $dataAc[$k]['start_time'] = date('Y-m-d H:00',$v['start_time']);
                 
                 if($v['type_id']==102){
                     $sqlQ ="SELECT q_content,sort FROM xyh_activity_link_vote_q WHERE activity_id = {$v['id']} limit 3 ";
                     $dataQ = Yii::app()->db->createCommand($sqlQ)->queryAll();
                     
                     $dataAc[$k]['q_content'] = $dataQ;
                 }
            }
              //热点
            $sqlHot = "SELECT a.id,a.title,a.start_time,a.type_id,a.banner_url FROM xyh_activity as a"
                         . " WHERE a.status=1 and a.is_del=2   and  a.is_hot=1";
            $dataHot = Yii::app()->db->createCommand($sqlHot)->queryRow();
            
            $dataHot['content']  =mb_substr(strip_tags($this->trimall($dataAc[$k]['content'])),0,86,'utf-8'); 
            $dataHot['start_time'] = date('Y-m-d H:00',$v['start_time']);
            
            
           // p_e($dataHot);

	    $this->render('index',array(
                
                'Activity'=>$dataAc,      
                'pages'=>$pages,
                'dataHot'=>$dataHot
                
                    ));
        
      
    }
    
   
    
    function trimall($str)//删除空格
        {
            $qian=array(" "," ","\t","\n","\r","&nbsp;");
            $hou=array("","","","","","");
            return str_replace($qian,$hou,$str);    
        } 
    
      function  TimeDifference($start,$end){
            
            if(($end -$start)>0){
                
                $day = floor(($end-$start)/86400);
               // $hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
               // $minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
               // $second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
                return $day;
            }else{
                
                return FALSE;
                
            }
        }
    
        
        
         public function actionView($ActivityId,$type_id){
       
         if(isset($_POST['ActivityVote'])){
       
                //p_e($_POST);
             $ACount='';
             $transaction  =Yii::app()->db->beginTransaction();
             $ActivityVote = new ActivityVote();
             
             foreach ($_POST['ActivityVote'] as $k=>$v){                
                 $vote_q_id = $k;
                 $vote_a_id = $v;
                   
                 $ActivityVote->vote_q_id = $vote_q_id;
                 $ActivityVote->vote_a_id = $vote_a_id;
                 $ActivityVote->ctime = time();
                 $ActivityVote->activity_id = $ActivityId;
                 
                 $ACount .=$vote_a_id.',';
                 if($ActivityVote->save()){
                      $ActivityVote = new ActivityVote();
                 }else{
                     
                     Yii::app()->user->setFlash('vote-form',$ActivityLinkVoteA->getErrors());
                     $transaction->rollback();
                     barek;
                     
                 }
             }
             
             
             
             $ACount = substr($ACount, 0, -1);
             $sqlACount = "UPDATE xyh_activity_link_vote_a SET vote_count=vote_count+1 WHERE id in ($ACount)";
             
             $sqlActivity = "UPDATE xyh_activity SET vote_count = vote_count+1 WHere id ={$ActivityId}";
             $res = Yii::app()->db->createCommand($sqlACount)->execute();
             
             if($res){
                   $res = Yii::app()->db->createCommand($sqlACount)->execute(); //活动表更新
                   if($res){
                       $transaction->commit();
                       Yii::app()->user->setFlash('vote-form', "提交完成！");
                       Yii::app()->jump->success("投票成功");
                      
                   }else{
                       
                       $transaction->rollback();
                        Yii::app()->jump->error("投票失败");
                       $this->refresh();
                   }
                   
             }else{
               
                 $transaction->rollback();
                 Yii::app()->jump->error("投票失败");
                 $this->refresh();
             }
             
           
           
             
             //p_e($_POST['ActivityVote']);
         }
             
        if($ActivityId){
            if($type_id =='101'){
                 $sql = "SELECT a.id,a.title,als.content,a.start_time,a.end_time,a.type_id,als.address,als.tel,als.contacts,a.min_banner_url,a.fav_count,a.signup_count,t.name,al.rename FROM xyh_activity as a
                    LEFT JOIN xyh_activity_link_signup as als on a.id = als.activity_id
                    LEFT JOIN xyh_type as t on a.cat_id = t.id
                    LEFT JOIN xyh_alumni as al on al.id = a.alumni_id
                    WHERE a.status=1 and a.is_del=2 and a.id={$ActivityId}";
                     $dataNew = Yii::app()->db->createCommand($sql)->queryAll();      
                     $this->render('view',array('dataNew'=>$dataNew));
            }
              if($type_id =='102'){       
                  $ActivityVote = new ActivityVote();
                  $ActivityVote->unsetAttributes();
                  
                 $sql=" SELECT ac.id as ac_id,ac.ctime,ac.type_id,ac.fav_count,ac.signup_count,ac.title,q.id AS q_id,q.q_content,q.type_id AS q_type_id,
                               al.rename,

                (SELECT GROUP_CONCAT(CONCAT(a.id,'-',a.a_content)) FROM xyh_activity_link_vote_a a 
                WHERE a.vote_q_id=q.id AND a.activity_id={$ActivityId}) AS a_info 
                FROM xyh_activity_link_vote_q AS q 
                LEFT JOIN xyh_activity as ac on ac.id = q.activity_id
                 LEFT JOIN xyh_alumni as al on ac.alumni_id = al.id
                WHERE q.activity_id={$ActivityId}
                GROUP BY q.id ORDER BY q.sort ASC";
                  
            
                   $dataNew = Yii::app()->db->createCommand($sql)->queryAll();  
                    
                   
                   // p_e($dataNew);
                    if($dataNew){
                        foreach ($dataNew as $k=>$v){
                         
                           $dataNew[$k]['a_info']=array();
                           $tmpinfo =explode(',',$v['a_info']);
                           foreach ($tmpinfo as $vv ){
                                 $tmp = explode('-',$vv);   
                                 $dataNew[$k]['a_info'][$tmp[0]] = $tmp[1];   
                            }
                        }  
                      
                    }               
                     $this->render('view',array('dataNew'=>$dataNew,'ActivityVote'=>$ActivityVote));
              }
             
        }
        
        
        
    }
    
     /**
     * 获取活动相关sql
     * @param int $user_id  用户ID
     * @return array 
     */
    
     public static function getActivitySqlArgs($activity_id){
            $time = time();
            $sql_args = array(
                'fields' => "a.id AS activity_id,a.type_id AS activity_type_id,a.title AS activity_title,"
                            . "a.start_time AS activity_start_time,a.end_time AS activity_end_time,"
                            . "al.rename AS alumni_name,t.name AS activity_cat_name,"
                            . "a.fav_count,a.signup_count,IF(v.id>0,1,2) as is_vote,IF(f.id>0, 1 ,2) AS is_fav",
                'case' => "CASE a.type_id "
                            . "WHEN 101 THEN "
                                . "(SELECT ls.address FROM xyh_activity_link_signup as ls WHERE ls.activity_id=a.id) "
                            . "WHEN 102 THEN "
                                . "(SELECT GROUP_CONCAT(q.q_content SEPARATOR '|||') FROM xyh_activity_link_vote_q as q WHERE q.activity_id=a.id AND q.sort<4) "
                            . "END AS activity_case_info ",
                'tables' => "FROM xyh_activity AS a "
                            . "LEFT JOIN xyh_alumni AS al ON a.alumni_id=al.id "
                            . "LEFT JOIN xyh_type AS t ON a.cat_id=t.id ",
                            //. "LEFT JOIN {{activity_vote}} AS v ON v.user_id={$user_id} AND v.activity_id=a.id "
                           // . "LEFT JOIN {{user_fav_activity}} AS f ON f.activity_id=a.id AND f.user_id={$user_id} AND f.is_del=2 ",
                'where' => "WHERE a.status=1 AND a.is_del=2 AND a.end_time>{$time} AND al.status=1 AND al.is_del=2 AND t.sign=11",
                'group' => "GROUP BY a.id ",
            );
            return $sql_args;
        }
    
    
        

    
    
}





